package com.obor.quartz.job.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.zn.mvcbase.page.PaginationUtility;

import com.obor.quartz.job.JobInfo;

/**
* 
* @Company OBOR
* @Author 周楠
* @Description
* 2019年4月10日
*
*/
@Service
public class MongoService {
	
	@Autowired
	 protected MongoTemplate mongoTemplate;
	 
	 
	@SuppressWarnings("rawtypes")
	public List<Map> queryTest() {
		
	/*	Query query = new Query(Criteria.where("name").is("").and("size").is("").and("age").is(""))
                .skip(10)
                .limit(10);*/

		
		List<Map> 	list =mongoTemplate.findAll(Map.class, "quartz_job");
		
		return list;
		
	}
	
	
	@SuppressWarnings("rawtypes")
	public List<Map> statusList(PaginationUtility<?> pu,JobInfo jobinfo){
		
		Query query = new Query(Criteria.where("key").is(jobinfo.getKey()).and("group").is(jobinfo.getGroup()))
                .skip(pu.getStartRow())
                .limit(pu.getPageSize());
		
		List<Map>  list= mongoTemplate.find(query, Map.class, "JobStatus");
		
		
		return list;
	}
	
	
	public JobInfo findJobStatus(JobInfo jobinfo){
		
		Query query = new Query(Criteria.where("key").is(jobinfo.getKey()).and("group").is(jobinfo.getGroup()));
		    
		JobInfo  ji= mongoTemplate.findOne(query, JobInfo.class, "JobStatus");
		
		return ji;
	}
	
	public List<Map> findJobLogs(PaginationUtility<?> pu,JobInfo jobinfo){
		
		Query query = new Query(Criteria.where("key").is(jobinfo.getKey()).and("group").is(jobinfo.getGroup()))
		.skip(pu.getStartRow())
        .limit(pu.getPageSize());
		    
		List<Map>  list =mongoTemplate.find(query, Map.class, "JobLogs");
		
		return list;
	}
	
	
	
	public void jobStatsUpdate(JobInfo jobinfo) {
		
		mongoTemplate.updateFirst(new Query(Criteria.where("key").is(jobinfo.getKey()).and("group").is(jobinfo.getGroup())),
				new Update().set("status", jobinfo.getStatus()).set("updateTime", new Date()), "JobStatus");
		
	}
	
	
	public void insertStatus(JobInfo jobinfo) {
		
		mongoTemplate.insert(jobinfo, "JobStatus");
		
	}
	
	public void insertLogs(Map<String, Object> map ) {
		
		mongoTemplate.insert(map, "JobLogs");
		
	}
	
	

}
